import api from "../common/api";
import p from "../utils/platform";
import constant from "./constant";
import { gioFissionLog } from "./gioHandler";
import { VIP_MESSAGE } from "./menuId";

/**
 * 权限校验
 * @param {*} menuId 操作id
 * @param {*} times 操作几次
 * @returns
 */
export function menuHandler(menuId, times = 1) {
  return new Promise((resolve, reject) => {
    api.getMemberPower(null, ({ data }) => {
      // debugger
      if (!data.isSuccess) return reject();

      const menuList = data.content.rows;
      wx.setStorageSync("memberInfo", menuList);

      let item = menuList.find((v) => v.menuId == menuId);
      if (!item) return reject();

      // item = {
      //   menuId,
      //   isPower: 0,
      //   statue: "3",

      //   dayVerify: true,
      //   dayNumber: 1,
      //   dayNumbered: 5,

      //   totalVerify: true,
      //   totalNumber: 0,
      //   totalNumbered: 10,
      // };

      // 总上限次数
      const totalMax = item.totalNumber + item.totalNumbered;
      // 当日总上限次数
      const dayMax = item.dayNumber + item.dayNumbered;

      // item.statue = 3
      switch (Number(item.statue)) {
        // 正常会员
        case 0:
          // 总次数不够
          if (item.totalVerify && item.totalNumber < times) {
            return reject(
              p.showModal({
                title: "系统提示",
                content: `${VIP_MESSAGE[menuId]}功能总使用上限为${totalMax}次，${
                  times == 1 ? "您已达使用次数上限" : "剩余可用次数不够" + times + "次"
                }`,
                confirmText: "我知道了",
                showCancel: false,
              })
            );
          }

          // 当日次数不够
          if (item.dayVerify && item.dayNumber < times) {
            return reject(
              p.showModal({
                title: "系统提示",
                content: `${VIP_MESSAGE[menuId]}功能当天使用上限为${dayMax}次，${
                  times == 1 ? "您已达当天使用次数上限" : "当天可用次数不够" + times + "次"
                }`,
                confirmText: "我知道了",
                showCancel: false,
              })
            );
          }

          // 正常使用
          return resolve(item);
          break;

        // 会员已禁用
        case 1:
          return reject(
            p
              .showModal({
                title: "系统提示",
                content: "您的会员已被禁用，请联系客服解禁",
                confirmText: "我知道了",
                cancelText: "联系客服",
              })
              .then(({ confirm }) => {
                if (!confirm) {
                  // 弹出 客服弹窗
                  this.setData({
                    kefu: true,
                    kefuInfo: {
                      close: () => {
                        this.setData({ kefu: false });
                      },
                    },
                  });
                  return Promise.reject()
                } else {
                  return Promise.resolve()
                }
              })
          );

          break;

        // 会员过期
        case 2:
        // 普通用户
        case 3:
          let _title = Number(item.statue) == 2 ? "续费" : "开通";
          const scene = Number(item.statue) == 2 ? constant.renewmember : constant.openmember
          
          let cb = (content) => {
            return new Promise((resolve, reject) => {
              this.setData({
                vipPopVisible: true,
                vipPopInfo: {
                  content,
                  cancelText: "残忍放弃",
                  confirmText: `立即${_title}`,
                  success: () => {
                    wx.navigateTo({
                      url: "/pages/mine/memberCentre/memberCentre",
                    });
                  },
                  fail: () => {
                    resolve();
                    this.setData({
                      vipPopInfo: {
                        showGuide: true,
                        success: () => {
                          wx.navigateTo({
                            url: `/pages/mine/newInvite/newFissionPage/newFissionPage?actEntre=${constant.abandonpay}`,
                          });

                          gioFissionLog(constant.goSeeFissionBtn, scene)
                        },
                        error:()=>{
                          this.setData({ vipPopVisible: false });
                        },
                      }
                    })
                    
                    gioFissionLog(constant.rejectVipBtn, scene)
                  },
                },
              });
            });
          };

          // 过期则不给用了
          // if (Number(item.statue) == 2) {
          //   return reject(cb('您的会员已过期！'));
          // }

          // 总次数不够
          if (item.totalVerify && item.totalNumber < times) {
            let content = `${VIP_MESSAGE[menuId]}功能为会员专属功能，|您已经使用${item.totalNumbered}次，还剩余${item.totalNumber}次，|${_title}会员即可立即使用`;
            return reject(cb(content));
          }

          // 当日次数不够
          if (item.dayVerify && item.dayNumber < times) {
            let content = `${VIP_MESSAGE[menuId]}功能为会员专属功能，|您当天已经使用${item.dayNumbered}次，当天还剩余${item.dayNumber}次，|${_title}会员即可立即使用`;
            return reject(cb(content));
          }

          // 不用校验
          if (!item.dayVerify && !item.totalVerify) {
            return resolve(item);
          }

          let tips = item.dayVerify ? '当天' : ''
          // 可以使用 出弹层询问
          return this.setData({
            vipPopVisible: true,
            vipPopInfo: {
              content: `${VIP_MESSAGE[menuId]}功能为会员专属功能，|您${tips}已经使用${item.dayNumbered}次，${tips}还剩余${item.dayNumber}次，|是否确认使用？`,
              cancelText: "取消",
              confirmText: `确定`,
              extraText: "开通会员，任性使用",
              extraCb: () => {
                wx.navigateTo({
                  url: "/pages/mine/memberCentre/memberCentre",
                });
              },
              success: () => {
                this.setData({ vipPopVisible: false });
                resolve(item);
              },
              fail: () => {
                this.setData({ vipPopVisible: false });
                reject(Promise.resolve());
              },
            },
          });
          break;
      }
    });
  });
}
